package com.hw.dagl.service.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.hw.dagl.model.Sys_Role;
import com.hw.dagl.model.Sys_User_Log;
import com.hw.dagl.model.Vehicle;
import com.hw.dagl.service.SysUserLogService;
import com.hw.dao.utils.EntityTools;
import com.hw.dao.utils.OraclePageDaoBase;
import com.hw.dao.utils.Page;

@Service
public class SysUserLogServiceImpl implements SysUserLogService {

	@Resource
	private OraclePageDaoBase oraclePageDaoBase;

	@Override
	@Transactional(readOnly=false,rollbackFor=Exception.class)
	public boolean insertSysUserLog(Sys_User_Log log) {
		//String id="ulog"+findSysUserLogId();
		int j=0;
		StringBuffer sql = new StringBuffer(" insert into sys_user_log (logid,yhdh,ip,modelname,methoddesc,url,datetime,optype,detailisnot,lsh,hphm) "
				+ "values (SEQ_SYSUSERLOGID.nextval,?,?,?,?,?,?,?,?,?,?) ");
		int i = oraclePageDaoBase.update(sql.toString(),new Object[]{log.getYhdh(),log.getIp(),log.getModelname(),log.getMethoddesc(),log.getUrl(),
				log.getDateTime(),log.getOptype(),log.getDetailisnot(),log.getLsh(),log.getHphm()});
	 if(log.getDetailisnot()!=null && log.getDetailisnot().equals("1")){
		StringBuffer sql2 = new StringBuffer("insert into sys_user_log_ref (logid,lsh,xh,hphm,hpzl,syr,clsbdh,ywmc,clpp1,tpurl,ywlx,ywyy) values (SEQ_SYSUSERLOGID.CURRVAL,?,?,?,?,?,?,?,?,?,?,?) ");
		 j = oraclePageDaoBase.update(sql2.toString(), new Object[]{log.getLsh(),log.getXh(),log.getHphm(),log.getHpzl(),log.getSyr(),log.getClsbdh(),
				 log.getYwmc(),log.getClpp1(),log.getTpurl(),log.getYwlx(),log.getYwyy()});
		 return i==j && i>= 1;
	 }
	   return i>= 1;
		
	}

	private String findSysUserLogId() {
		String sql = " select SEQ_SYSUSERLOGID.nextval from dual ";
		String sysUserLogId  = oraclePageDaoBase.queryForObject(sql, new Object[]{},String.class);
		return sysUserLogId;
	}

	@Override
	public Page<Sys_User_Log> findSysuserlog(Page<Sys_User_Log> data, Sys_User_Log info) {
		StringBuffer sql = new StringBuffer(" select h.*,decode(h.hphm,'','','皖'||h.hphm) hphmmc,decode(h.optype,'0','登录','1','新增','2','修改','3','查询','4','删除','5','下载','9','登出') optypemc "
				+ "from Sys_User_log h where 1=1 ");

		List<Object> args = new ArrayList<Object>();
		if(StringUtils.isNotEmpty(info.getYhdh())){
			sql.append(" AND h.yhdh = ? ");
			args.add(info.getYhdh());
		}
		if(StringUtils.isNotEmpty(info.getIp())){
			sql.append(" AND h.ip = ? ");
			args.add(info.getIp());
		}
		if(StringUtils.isNotEmpty(info.getUrl())){
			sql.append(" AND h.url = ? ");
			args.add(info.getUrl());
		}
		if(StringUtils.isNotEmpty(info.getOptype())){
			sql.append(" AND h.optype = ? ");
			args.add(info.getOptype());
		}
		if(StringUtils.isNotEmpty(info.getLsh())){
			sql.append(" AND h.lsh = ? ");
			args.add(info.getLsh());
		}
		if(StringUtils.isNotEmpty(info.getHphm())){
			sql.append(" AND h.hphm = ? ");
			args.add(info.getHphm());
		}
		SimpleDateFormat formatDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		if(StringUtils.isNotEmpty(info.getDateTimeStartStr())) {
			sql.append(" AND h.datetime >= ? ");
			try {
				args.add(formatDate.parse(info.getDateTimeStartStr()));
			} catch (ParseException e) {
				e.printStackTrace();
			}
		}
		if(StringUtils.isNotEmpty(info.getDateTimeEndStr())) {
			sql.append(" AND h.datetime <= ? ");
			try {
				args.add(formatDate.parse(info.getDateTimeEndStr()));
			} catch (ParseException e) {
				e.printStackTrace();
			}
		}

		sql.append(" order by h.datetime desc");
		return oraclePageDaoBase.queryForPage(sql.toString(), data, new RowMapper<Sys_User_Log>(){
			private EntityTools<Sys_User_Log> et = new EntityTools<Sys_User_Log>();
			@Override
			public Sys_User_Log mapRow(ResultSet rs, int arg1)throws SQLException {
				Sys_User_Log log = new Sys_User_Log();
				et.setColumnsValueByResultSet(rs, log);
				return log;
			}
		}, args.toArray());
	}

	public Vehicle findYxxxglByxh(Vehicle info, String startTime, String endTime) {
	    StringBuffer sql=new StringBuffer("select g.*,hp.dmsm1 hpzlmc from "
	    		+ "(select r.*,'皖'||r.hphm hphmmc from SYS_USER_LOG t,SYS_USER_LOG_REF r where t.logid=r.logid ");
	    if (StringUtils.isNotEmpty(info.getLogid())) {
	    	sql.append(" AND t.logid =  '" + info.getLogid()+ "'");
	    }	
       sql.append(") g,(select t.* from Frm_Code t where 1=1 and t.dmlb='1007' and t.xtlb='00') hp where hp.dmz=g.hpzl(+)");
	    //queryForObject在查询不到数据的时候会报空指针异常
	    List<Vehicle> vehicles=oraclePageDaoBase.query(sql.toString(),new BeanPropertyRowMapper<Vehicle>(Vehicle.class));
		if(vehicles.size()>0){
			 info=vehicles.get(0);
		}else{
			info=new Vehicle();
		}
		 return info;
	}


}
